/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Practica6.practica;

import Practica6.structures.DisjointSet;

/**
 *
 * @author miguel
 */
public class MyDisjointSet extends DisjointSet{
    
    public MyDisjointSet(int numElements){
        vertices = new int[numElements];
        representantes = new int[numElements];
        for(int i=0;i<vertices.length;i++){
            vertices[i] = -1;
            representantes[i] = -1;
        }
        
    }
    
    public void makeSet(int vertex){
        
    }

    @Override
    public int findAgent(int value) {
        if(vertices[value]<0){
            return value;
        }else{
            vertices[value] = findAgent(vertices[value]);
            return vertices[value];
        }
    }

    @Override
    public void union(int vertex1, int vertex2) {
        if(vertices[vertex1]<vertices[vertex2]){
            vertices[vertex1] = vertex2;
        }else{
            if(vertices[vertex1] == vertices[vertex2])
                vertices[vertex1]--;
            vertices[vertex2] = vertex1;
        }
        
    }

    @Override
    public String toString() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    public static void main(String[] args) {
    int NumElements = 128;
    int NumInSameSet = 16;

    MyDisjointSet s = new MyDisjointSet(NumElements);
    int set1, set2;
    for (int k = 1; k < NumInSameSet; k *= 2) {
      for (int j = 0; j + k < NumElements; j += 2 * k) {
        set1 = s.findAgent(j);
        set2 = s.findAgent(j + k);
        s.union(set1, set2);
      }
    }

    for (int i = 0; i < NumElements; i++) {
      System.out.print(s.findAgent(i) + "*");
      if (i % NumInSameSet == NumInSameSet - 1) {
        System.out.println();
      }
    }
    System.out.println();
  }
    
}
